package com.inray.nbs.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.inray.nbs.dao.BaseDao;


@Repository
public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> {
	@Resource(name = "sqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    @PostConstruct
    public void initSqlMapClient(){
        super.setSqlSessionFactory(sqlSessionFactory);
    } 
    
	@Override
	public int insert(T obj) {
		return getSqlSession().insert(obj.getClass().getSimpleName()+".insert",obj);
	}
	
	@Override
	public int insertBySqlId(String sqlId,Object obj) {
		return getSqlSession().insert(sqlId,obj);
	}

	@Override
	public int delete(Class<T> clazz,int id) {
		return getSqlSession().delete(clazz.getSimpleName()+".delete",id);
	}
	
	@Override
	public int deleteBySqlId(String sqlId,Object obj) {
		return getSqlSession().delete(sqlId,obj);
	}

	@Override
	public int update(T obj) {
		return getSqlSession().update(obj.getClass().getSimpleName()+".update",obj);
	}
	
	@Override
	public int updateBySqlId(String sqlId,Object obj) {
		return getSqlSession().update(sqlId,obj);
	}

	@Override
	public T selectOneById(Class<T> clazz,int id) {
		return getSqlSession().selectOne(clazz.getSimpleName()+".selectOne",id);
	}

	@Override
	public T selectOneBySqlId(String sqlId, Object obj) {
		return getSqlSession().selectOne(sqlId, obj);
		
	}

	@Override
	public List<T> selectList(Class<T> clazz) {
		return getSqlSession().selectList(clazz.getSimpleName()+".selectAll");
	}

	@Override
	public List<T> selectListBySqlId(String sqlId, Object obj) {
		return getSqlSession().selectList(sqlId,obj);
	}
	
	@Override
	public Map<String, Object> selectMapBySqlId(String sqlId,Object obj,String mapKey){
		return getSqlSession().selectMap(sqlId,obj,mapKey);
	}

	@Override
	public Object selectOne4ObjBySqlId(String sqlId, Object obj) {
		return getSqlSession().selectOne(sqlId, obj);
	}

	@Override
	public List<Object> selectList4ObjBySqlId(String sqlId, Object obj) {
		return getSqlSession().selectList(sqlId,obj);
	}
	
}
